﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace HanGu.CRM.Areas.CustomerInfo.Controllers
{
    public class ChangeGradeRecordeController : AdminBaseController
    {
        //
        // GET: /ChangeGradeRecorde/

        public ActionResult Index(int id)
        {
            return View(GetChangeGradeRecordeList(id));
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            int id = 0;
            int.TryParse(Request["VIPID"], out id);
            return View(GetChangeGradeRecordeList(id));
        }

        List<VChangeGradeRecorde> GetChangeGradeRecordeList(int id)
        {
            SearchParma.IntObj1 = id;
            List<VChangeGradeRecorde> ChangeGradeRecordeList = serviceUpGrade.GetAll(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return ChangeGradeRecordeList;
        }

        SelectList GetListFromDict(string groupName, bool IsSelectLabel = false)
        {
            var lst = serviceDict.GetByGroupName(groupName);
            if (IsSelectLabel)
            {
                lst.Insert(0, new HanGu.Entity.Dictionary() { key = string.Empty, Value = HelperCommon.SelectDefaultValue });
            }
            SelectList sList = new SelectList(lst, "GenerateKey", "Value");
            return sList;
        }

        public ActionResult Create(int id)
        {
            ViewData["Degree"] = GetListFromDict("Degree");
            ViewData["MemberInfo"] = TempData["MemberInfo"] = serviceMemberInfo.GetByID(id);
            ChangeGradeRecorde changeGradeRecorde = new ChangeGradeRecorde() { VIPID = id, ChangeTime = DateTime.Now, Changer = CurentUser.UserName };
            return View(changeGradeRecorde);
        }

        //
        // POST: /ChangeGradeRecorde/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            ChangeGradeRecorde changeGradeRecorde = new ChangeGradeRecorde();
            try
            {
                var memberinfo = TempData["MemberInfo"] as MemberInfo;
                if (memberinfo != null)
                {
                    TryUpdateModel(changeGradeRecorde, collection);
                    if (serviceUpGrade.Add(changeGradeRecorde))
                    {
                        memberinfo.Degree = changeGradeRecorde.ChangeValue;
                        if (serviceMemberInfo.Update(memberinfo))
                        {
                            navTabId = "CustomerInfoDefault";
                            ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                        }
                        else
                        { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }

                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }

        

        //
        // GET: /ChangeGradeRecorde/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {                
                serviceUpGrade.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
